var orderPlugs = function (fid) {
    var elements = null;
    var column = null;
    var order = null;
    var formparentId = "#query_form";

    function init(fid) {
        if(fid != undefined && fid != ""){
            formparentId = fid;
        }
        console.log($(formparentId).find(".orderPlugs-column"));
        column = $($(formparentId).find(".orderPlugs-column")[0]).attr("value");
        order = $($(formparentId).find(".orderPlugs-order")[0]).attr("value");
        console.log(column);
        console.log(order);
        elements = $(".orderPlugs");
        var elw = $($(elements[0]).parent()).innerHeight;
        var elh = $($(elements[0]).parent()).innerHeight() / 3;
        elements.each(function (i, el) {
            console.log($($(el).parent()));
            $(el).css({
                "width":elw
                ,"height": elh
                , "display": "inline-block"
                ,"padding-right":10
                ,"padding-left":10
                ,"line-height":elh

            });
            $(el).html("<img src='../up.png' style='width:" + (el.clientHeight  ) + "px;height:" + (el.clientHeight) + "px;float:left;' class='asc' />" +
                "<img src='../down.png' style='width:" + (el.clientHeight  ) + "px;height:" +( el.clientHeight )+ "px;float:left;' class='desc' />");
            $(el).on("click", function (event) {
                orderClick(this);
            });

            if (column == $(el).attr("data-column")) {
                order == "asc" ? $($(el).find(".asc")).hide() : $($(el).find(".desc")).hide();
            }
        });
    }

    function orderClick(el) {
        $($(formparentId).find(".orderPlugs-column")[0]).val($(el).attr("data-column"));
        if(order == ""){
            $($( formparentId).find(".orderPlugs-order")[0]).val("asc");
        }else if(order.toLowerCase() == "asc"){
            $($(formparentId).find(".orderPlugs-order")[0]).val("desc");
        }else{
            $($( formparentId).find(".orderPlugs-order")[0]).val("");
            $($( formparentId).find(".orderPlugs-column")[0]).val("");
        }
        $( formparentId).submit();
    }

    init(fid);
};